const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 在 api 文件夹下创建 request.js 文件
const baseURL = "http://admin.bsmall.byesame.com/wx/";
export function axios(options) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + options.url,
      data: options.data || {},
      method: options.method || 'GET',
      header: options.header || {
        "X-Byesame-Token": wx.getStorageSync('token')
      },
      success: res => {
        resolve(res);
      },
      fail: err => {
        reject(err);
      }
    });
  });
}
export function  getitime(){
	let data = new Date().getTime()
	wx.setStorage('flag',false)
	if(wx.getStorageSync('flag')){
		wx.setStorageSync('timenow', data)
	}else{
		wx.setStorageSync('flag',true)
		wx.setStorageSync('timeout',data)
	}
}
export function  getendtime(fun){
	let c = new Date().getTime()
	if(wx.getStorageSync('timenow')-wx.getStorageSync('timeout') > 180000  || !wx.getStorageSync('token')){
		console.log(2);
		wx.showToast({
			title: '请先登录',
			icon:"error",
			mask:true,
			duration:2500,
			success:()=>{
				console.log(123);
				setTimeout(() => {
					wx.redirectTo({
						url: '/pages/login/login',
					})
				}, 2000);
			}
		})
	
		wx.removeStorageSync('token')
		wx.removeStorageSync('timeout')
	}else{
			fun
	}
}
module.exports = {
  formatTime,
	axios,
	getendtime,
	getitime
}
